//
//  DynamicLoginViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/4/18.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "DynamicLoginViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "DynameicLoginView.h"
#import "CommonMethod.h"
#import "BsTenantsViewModel.h"
@interface DynamicLoginViewController ()

@property (nonatomic ,strong) DynameicLoginView *dynameicLoginView;

//** 获取验证码的时间*/
@property (assign, nonatomic) NSInteger allDuration;
@property (strong, nonatomic) NSTimer *timer;

// ** 验证码*/
@property (nonatomic ,strong) NSString *verificationCode;

@end

@implementation DynamicLoginViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"忘记密码";
    [self setNarBar];
    [self createUI];
}

// 发送验证码
- (void)loadDataSendCode {
    
    NSString *userPhone = self.dynameicLoginView.userPhone.text;
    //http://118.190.98.64/tctxAPI/reg_user .aspx?phone=手机号
    [BsTenantsViewModel loadSendVerificationCodeNetWorking:userPhone andReturnValue:^(id returnValue) {
        NSLog(@"获取验证码：%@",returnValue);
        self.verificationCode = [NSString stringWithFormat:@"%@",returnValue];
        
    } andReturenError:^(id error) {
        NSLog(@"未成功%@",error);
        [self showAlertWithTitle:@"发送失败！"];
    }];
    
    
}
// 提交验证码
- (void)loadDataSubmitCode {
    
    NSString *userPhone = self.dynameicLoginView.userPhone.text;
    NSString *validationCode = self.dynameicLoginView.verificationCode.text;
    NSString *urlString = [NSString stringWithFormat:@"%@/Verification_code.aspx",serverUrl];
    NSDictionary *paramDic = @{@"phone":userPhone,@"code":validationCode};
    [NetworkManager requestPOSTWithURLStr:urlString paramDic:paramDic finish:^(id responseObject) {
        NSLog(@"请求成功%@",responseObject);
        if (RESPONSEOBJECT) {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提交验证码" message:@"成功" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            
            [alertController addAction:okAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }else{
            [self showAlertWith:responseObject[@"message"]];
        }
        
    } enError:^(NSError *error) {
        NSLog(@"请求失败%@",error);
    }];
}


-(void)setNarBar {
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithHexString:@"#ff9900"]];
    //1.3.1 在具有导航栏的viewController中设置
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem  itemWithTitle:@"返回" ImageName:@"fanhui" highImageName:nil target:self action:@selector(navItemClick:)];
    
}

#pragma mark - 返回
- (void)navItemClick:(UIBarButtonItem *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)createUI {
    
    self.dynameicLoginView = [[DynameicLoginView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_dynameicLoginView];

    YXWeakSelf
#pragma mark - 获取验证码
    self.dynameicLoginView.sendCodeBtnClick = ^{
      
        if ([CommonMethod validatePhone:weakSelf.dynameicLoginView.userPhone.text]) {
            _allDuration = 60;
            weakSelf.dynameicLoginView.sendCodeBtn.enabled = NO;
            [weakSelf.dynameicLoginView.sendCodeBtn setTitle:[NSString stringWithFormat:@"%@秒后重新获取",@(weakSelf.allDuration)] forState:(UIControlStateNormal)];
            _timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(timeChanged:) userInfo:nil repeats:YES];
            [weakSelf loadDataSendCode];
        }else{
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请输入11位手机号" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        }];
            
            [alertController addAction:okAction];
            
            [weakSelf presentViewController:alertController animated:YES completion:nil];
        
        }
    };
    
#pragma mark - 确认提交
    self.dynameicLoginView.submitBtnClick = ^{
        [weakSelf loadDataSubmitCode];
        
    };
}

- (void)timeChanged:(NSTimer*)timer{
    
    _allDuration--;
    [self.dynameicLoginView.sendCodeBtn setTitle:[NSString stringWithFormat:@"%@秒",@(_allDuration)] forState:UIControlStateNormal];
    if (_allDuration ==0) {
        [self.dynameicLoginView.sendCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.dynameicLoginView.sendCodeBtn.enabled = YES;
        [self.timer invalidate];
        self.timer =nil;
    }
}


@end
